package futian 
{
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.utils.clearTimeout;
	import flash.utils.setTimeout;
	import flash.utils.getTimer;

	public class PhotoPlayer extends Sprite
	{
		private var stageW:int;//the stage width
		private var stageH:int;//the stage height
		private var pathArr:Array=null;//path Array of pictures;
		private var indexInArr:int=0;//the index of item in the pathArr

		//number of pictures per loop;//diferent loop different in and out effect;//after these loops ended, it will show a 3d circle;//after 3d effect,it will show 3x5 effect,// at the end of 3x5 effect, restart this effect of this class;
		public function get maxPhotos():int
		{
			CONFIG::debugging{//for debugging version; i set it a minimun value;
				return 2;
			}
			return 8;
		}
		private var photoIndex:uint = 0;
		public function PhotoPlayer(_w:int,_h:int)
		{
			stageW = _w;
			stageH = _h;
			//x = 1920/4;
			//y = 1080/4;
			addPhotoPath(null);
			//playIndex(indexInArr);
		}


		private var bigBmp:Bitmap;
		private var timeoutId5s:uint;
		public function show5s(path:String):void
		{
			clearTimeout(timeoutId5s);
			timeoutId5s = setTimeout(remove5s,5000);

			remove5s();
			SwfLoader.SwfLoad(path,loaded5s);
		}
		private function loaded5s(e:Event):void
		{
			if(e && Event.COMPLETE == e.type)
			{
				bigBmp = e.target.content as Bitmap;
			}
			if(bigBmp && stage){
				stage.addChild(bigBmp);
				if(bigBmp.width>stage.stageWidth/2)
				{
					bigBmp.width = stage.stageWidth/2;
					bigBmp.scaleY = bigBmp.scaleX;
				}
				bigBmp.x = stage.stageWidth/2 - bigBmp.width/2;
				bigBmp.y = stage.stageHeight/2 - bigBmp.height/2;
			}
		}

		private function remove5s():void
		{
			if(bigBmp)
			{
				if(bigBmp.parent)
					bigBmp.parent.removeChild(bigBmp);
				bigBmp.bitmapData.dispose();
				bigBmp = null;
			}
		}

		private function playNext():void
		{
			++indexInArr;
			if(pathArr==null || indexInArr>=pathArr.length)
				indexInArr = 0;
			playIndex(indexInArr);
		}

		private function isPathIn(path:String):Boolean
		{
			var list:Array = pathArr;
			if(list==null || list.length==0 || path==null || path.length==0)
				return false;
			for each(var p:String in pathArr)
			{
				if(p==path)
					return true;
			}
			return false;
		}

		public static function sortFun(file1:String,file2:String):int
		{
			var arr1:Array = file1.match(/[0-9]+/g);
			var arr2:Array = file2.match(/[0-9]+/g);

			if(arr1 && arr2)
			{
				var n1:int = arr1.length;
				var n2:int = arr2.length;
				var i:int = 0;
				while(i<n1 && i<n2)
				{
					var num :Number = int(arr2[i]) - int(arr1[i]);
					if(num > 0)return 1;
					if(num < 0)return -1;
					++i;
				}
			}

			return 0;
		}

		public function addPhotoPath(path:String=null,toFirstPos:Boolean=false):int
		{
			if(pathArr==null){
				pathArr = WriteData.getFilePathsInDir("futian/images/","(\.png$)|(\.jpg$)");
				if(pathArr)
					pathArr.sort(sortFun);
			}
			if(path && path.length>0)
			{
				if(pathArr==null)
					pathArr = new Array;
				if(!isPathIn(path))
				{
					if(toFirstPos){
						pathArr.unshift(path);
						//playIndex(0);
					}else{
						pathArr.push(path);
					}
					//logs.adds(pathArr,pathArr.length);
					return pathArr.length-1;
				}
			}
			return indexInArr;
		}

		public function get totalTime():uint
		{
			var i:uint = (maxPhotos)*numloop*2000; 
			return i;
		}

		private var timeoutId:uint;
		private var justOut:Boolean = false;
		private const numloop:int = 4;
		private var roundplayer:RoundPlayer;
		private var player3x5:Player3x5;
		public function playIndex(i:int):void
		{
			clearTimeout(timeoutId);
			timeoutId = setTimeout(playNext,2000);

			if(pathArr==null || i>=pathArr.length)
			{
				i=0;
				return;
			}
			indexInArr = i;
			justOut = Boolean(int(photoIndex/maxPhotos)%numloop >= numloop-1);

			var _numChildren:int = numChildren;
			//while(_numChildren>=maxPhotos || _numChildren>=pathArr.length)
			if(justOut || _numChildren>=maxPhotos)
			{
				outLastPhoto();
				--_numChildren;
			}
			if(_numChildren==0 && justOut)
			{
				startRoundPlayer(indexInArr);
				return;
			}

			if(justOut)
			{
			}else{
				SwfLoader.SwfLoad(pathArr[i],loaded);
			}
		}

		public function replay():void
		{
			rotationX = 0;
			photoIndex = 0;
			playIndex(photoIndex);
		}

		private function startRoundPlayer(i:int):void
		{
			stop();
			if(roundplayer==null)
				roundplayer = new RoundPlayer(stageW,stageH);
			roundplayer.pathArr = pathArr;
			roundplayer.photoIndex = i;
			addChild(roundplayer);
		}

		public function play3x5(i:int):void
		{
			stop();
			if(player3x5==null)
				player3x5 = new Player3x5(stageW,stageH);
			player3x5.pathArr = pathArr;
			player3x5.photoIndex = i;
			player3x5.x = 0;
			player3x5.y = 0;
			addChild(player3x5);
		}

		private function loaded(e:Event):void
		{
			if(e && Event.COMPLETE == e.type)
			{
				var bmp:Bitmap = e.target.content as Bitmap;
				if(bmp){
					var mc:Sprite = new Sprite();
					mc.name = ""+photoIndex;
					mc.addChild(bmp);
					bmp.x = -bmp.width/2;
					bmp.y = -bmp.height/2;
					addChild(mc);
					//CONFIG::debugging
					{
						mc.width = stageW/8;
					}
					mc.scaleY = mc.scaleX;
					showUp(mc);
				}
			}
		}

		private function showUp(mc:Sprite):void
		{
			//logs.adds(mc.name,"in");
			var i:int = photoIndex;
			var ww:int = stageW/maxPhotos/1.1;
			var hh:int = stageH/4;
			var xx:int;
			var yy:int;
			var toX:int;
			var toY:int;
			var r:int = stageW/3;
			var angle:Number = 0;

			switch(int(i/maxPhotos)%numloop)
			{
				case 0:
					mc.x = - stageW - mc.width; 
					mc.y = (stageH - mc.height)*Math.random()+mc.height/2;
					xx = stageW/2-int(maxPhotos)*ww/2;
					yy = stageH/2-(i%2)*hh/2;
					toX = xx+(i%maxPhotos+1.0/2)*ww;
					toY = yy;
					TweenLite.to(mc,1.0,{x:toX,rotationZ:360,y:toY});
					break;
				case 1:
					mc.x = (stageW - mc.height)*Math.random()+mc.width/2;
					mc.y = - mc.height;
					xx = stageW/2-int(maxPhotos)*ww/2;
					yy = stageH/2-hh/4;
					toX = xx+(i%maxPhotos+1.0/2)*ww;
					toY = yy;
					TweenLite.to(mc,1.0,{x:toX,rotationY:360,y:toY});
					break;
				case 2:
					mc.x = (stageW - mc.height)*Math.random()+mc.width/2;
					mc.y = stageH + mc.height/2;
					xx = stageW/2-int(maxPhotos)*ww/2;
					yy = stageH/2-((i-1)%2)*hh/2;
					toX = xx+(i%maxPhotos+1.0/2)*ww;
					toY = yy;
					TweenLite.to(mc,1.0,{x:toX,rotationX:360,y:toY});
					break;
				case 5:
					mc.x = (stageW - mc.height)*Math.random()+mc.width/2;
					mc.y = stageH + mc.height/2;
					angle = Number(-i*1.0/maxPhotos)*Math.PI;
					xx = stageW/2-r*Math.cos(angle)/2;
					yy = stageH/2+r*Math.sin(angle);
					toX = xx;//+(i%maxPhotos+1.0/2)*hh;
					toY = yy/2+stageH/4;
					TweenLite.to(mc,1.0,{x:toX,rotationX:360,y:toY});
					break;
				case 4:
					mc.x = (stageW - mc.height)*Math.random()+mc.width/2;
					mc.y = stageH + mc.height/2;
					angle = Number(1+i*1.0/maxPhotos)*Math.PI;
					xx = stageW/2-r*Math.cos(angle)/2;
					yy = stageH/2+r*Math.sin(angle);
					toX = xx;//+(i%maxPhotos+1.0/2)*hh;
					toY = yy/2+stageH/4;
					TweenLite.to(mc,1.0,{x:toX,rotationX:360,y:toY});
					break;
			}
			photoIndex++;
		}

		private function outLastPhoto():void
		{
			if(numChildren>0)
			{
				var lastPhoto:Sprite = getChildAt(0) as Sprite;
				var i:int = 1;
				while(i<numChildren)
				{
					var photo:Sprite = getChildAt(i) as Sprite;
					if(int(photo.name)<int(lastPhoto.name))
					{
						lastPhoto = photo;
					}
					++i;
				}
				//logs.adds(lastPhoto.name,"out",numChildren);
				TweenLite.to(lastPhoto,.9,{x:stageW+lastPhoto.width,rotationY:360,onComplete:removeObj,onCompleteParams:[lastPhoto]});
				//TweenLite.to(lastPhoto,1.0,{alpha:0,onComplete:removeObj,onCompleteParams:[lastPhoto]});

			}
		}
		private function removeObj(mc:Sprite):void
		{
			if(mc)
			{
				if(mc.numChildren>0)
					ViewSet.removes(mc);

				if(mc.parent)
					mc.parent.removeChild(mc);
			}
		}
		public function stop():void
		{
			clearTimeout(timeoutId);
			logs.adds("stop");
		}

	}
}
